<html>

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var ou;



        var json = "";
        var ncbc = "", district = "", sub = "";
        var checkOuLevel = function (ou) {
            $.ajax({
                async: false,
                type: "GET",
                url: "../api/organisationUnits/" + ou + ".json",
                success: function (data) {
                    json = data;

                }
            });
            return json;
        };

        var month;
        var getMonth = function (a) {
            if (a == "1" || a == "01") { month = "January" }
            if (a == "2" || a == "02") { month = "February" }
            if (a == "3" || a == "03") { month = "March" }
            if (a == "4" || a == "04") { month = "April" }
            if (a == "5" || a == "05") { month = "May" }
            if (a == "6" || a == "06") { month = "June" }
            if (a == "7" || a == "07") { month = "July" }
            if (a == "8" || a == "08") { month = "August" }
            if (a == "9" || a == "09") { month = "September" }
            if (a == "10") { month = "october" }
            if (a == "11") { month = "November" }
            if (a == "12") { month = "December" }
        };

        var getFirstTwoColumn = function (ou) {
            $.ajax({
                async: false,
                type: "GET",
                url: "../api/organisationUnits/" + ou + ".json?fields=ancestors[id,name,level],displayName",
                success: function (data) {
                    for (var i = 0; i < data.ancestors.length; i++) {
                        if (data.ancestors[i].level == 2) {
                            district = data.ancestors[i].name;
                        }
                        ncbc = data.displayName;

                    }

                }
            });
            return district, ncbc;
        };

        $(document).ready(function () {
            var url = window.location.href;
            var pe1 = url.split("&")[1];
            var pe = pe1.split("=")[1];
            var monthCode = pe.substr(4, 6);
            getMonth(monthCode);
            var year = pe.substr(0, 4);
            var ou1 = url.split("&")[2];
            var ou = ou1.split("=")[1];
            var ouLeveldata = checkOuLevel(ou);
            var ouLevel = ouLeveldata.level;
            var index = 1;
            //  var periodArray = getPeriodArray(pe);

            $("#export").click(function (e) {
                e.preventDefault();

                //getting data from our table
                var data_type = 'data:application/vnd.ms-excel';
                var table_div = document.getElementById('report_table');
                var table_html = table_div.outerHTML.replace(/ /g, '%20');

                var a = document.createElement('a');
                a.href = data_type + ', ' + table_html;
                a.download = 'Contract Work Status.xls';
                a.click();
            });

            var printData = function (ou) {
                var json1 = ""; json2 = ""; json3 = "";

                var ncbc = "", drcn = "", sub = "", col1 = "", col2 = "", col3 = "", col4 = "", col5 = "", col6 = "", col7 = "", col8 = "";

                $.ajax({
                    async: false,
                    type: "GET",
                    dataType: "json",
                    contentType: "application/json",
                    url: "../api/26/analytics.json?dimension=dx:LCXfADQBVaI;QTLXq5UrnTX;i7uHppvTD1c;TWDgaEfNZas;Sw59EqwR2VW;QtHHGL7bH8N;b0WGsBAPVng;MyWgX64EmP2;Wwck92FsZKR;rsekYnlKyNZ;pZuxxyBA3wr;isGe8SeEEdw&dimension=ou:" + ou + "&filter=pe:" + pe + "&displayProperty=NAME&outputIdScheme=NAME",
                    success: function (data) {

                        for (var i = 0; i < data.rows.length; i++) {


                            if (data.rows[i][0] == "CWS_Back cutting") {
                                col1 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "CWS_Construction of road & off road structures") {
                                col2 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "CWS_Side drain cross drainage work") {
                                col3 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "CWS_Sub grade sub base work") {
                                col4 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "CWS_Laying and preparation of base work") {
                                col5 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "CWS_Pavement surface treatment work") {
                                col6 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "CWS_Road furniture safety work") {
                                col7 = data.rows[i][2];
                            }
                            if (data.rows[i][0] == "CWS_Remarks") {
                                col8 = data.rows[i][2];
                            }
                            sub = data.rows[i][1];
                        }
                        getFirstTwoColumn(ou);

                        var row = "<tr><td>" + district + "</td><td>" + sub + "</td><td>" + ncbc + "</td><td>" + col1 + "</td><td>" + col2 + "</td><td>" + col3 + "</td><td>" + col4 + "</td><td>" + col5 + "</td><td>" + col6 + "</td><td>" + col7 + "</td><td>" + col8 + "</td></tr >";
                        $('.reporttable').append(row);

                    }

                });

            };

            if (ouLevel == 1) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    var tempOuData = checkOuLevel(tempOu);
                    if (tempOuData.children.length !== 0) {
                        for (var l = 0; l < tempOuData.children.length; l++) {
                            var tempOu2 = tempOuData.children[l].id;
                            var tempOuData2 = checkOuLevel(tempOu2);
                            if (tempOuData2.children.length !== 0) {
                                for (var m = 0; m < tempOuData2.children.length; m++) {
                                    ouu = tempOuData2.children[m].id;
                                    printData(ouu);

                                }
                            }
                        }
                    }
                }
            }

            else if (ouLevel == 2) {

                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    var tempOuData = checkOuLevel(tempOu);
                    if (tempOuData.children.length !== 0) {
                        for (var l = 0; l < tempOuData.children.length; l++) {
                            ouu = tempOuData.children[l].id;
                            printData(ouu);
                        }
                    }
                }
            }
            else if (ouLevel == 3) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    ouu = ouLeveldata.children[p].id;
                    printData(ouu);
                }
            }
            else if (ouLevel == 4) {
                printData(ou);
            }


        });
    </script>
    <style>
        .heading td {
            background: #1D9FDF;
            color: white;
            text-align: center;
            border: none;
        }

        #heading {
            text-align: center;
        }


        table {
            border-collapse: collapse;
        }

        td {
            border: 1px solid black;
            height: 30px;
            width: 280px;
            text-align: center;
            font-weight: bold;
        }

        .noBorder {
            border: none;
        }

        table thead {
            background: gray;
        }
    </style>
</head>

<body>
    <div class="outer">
        <center><button id="export">Export to Excel</button></center>
        <br>
        <table class="table table-bordered reporttable" id="report_table"  border='1'>
            <thead>
                <tr class="heading">
                    <td colspan="3" rowspan="3"><img src="../dhis-web-commons/security/DRILP_Logo.jpg" width="90%" height="400%"></td>
                    <td colspan="11" rowspan="1">
                        <p style="font-size:16px;">Earthquake Emergency Assistance Project (EEAP)</p>
                    </td>
                    <td colspan="3" rowspan="3"></td>
                </tr>
                <tr class="heading">
                    <td colspan="11" rowspan="1">
                        <p style="font-size:20px;">Rehabilitation and Reconstruction of District Roads</p>
                    </td>
                </tr>
                <tr class="heading">
                    <td colspan="11" rowspan="1">
                        <p style="font-size:16px;">Contract Work Status</p>
                    </td>
                </tr>
                <tr>
                    <td colspan="11">Length of work (KM) executed / completed by the Contarctor</td>
                </tr>
                <tr>
                    <td>District</td>
                    <td>Sub-project:</td>
                    <td>NCB Contract:</td>
                    <td>Back cutting in roadways, E/W in Foundation and side drain</td>
                    <td>Construction of road and off road structures (Gabion & Masonry wall)</td>
                    <td>Side Drain and Cross Drainage work , Causway</td>
                    <td> Subgrade & Sub-base work</td>
                    <td>Laying and preparation of Base course </td>
                    <td>Pavement work/surface treatment (black top)</td>
                    <td>Road Furniture/Traffic sign and Traffic safety work</td>
                    <td>Remarks</td>
                </tr>
            </thead>
            <tbody>

            </tbody>
        </table>

    </div>
</body>

</html>